//
//  KFLMyListRequest.m
//  KWNLaywerClient
//
//  Created by 孔六五 on 15/1/23.
//  Copyright (c) 2015年 张明磊. All rights reserved.
//

#import "KFLMyListRequest.h"
#import "Model_myList.h"
@implementation KFLMyListRequest
@synthesize myList_displaynum;
@synthesize myList_startNum;

- (void)startToRequestWithBlock:(requestResultBlock)resultBlock;
{
    self.kflEngine.hostName = @"getanswinfo";

    NSString *string_uid = [[Model_user sharedInstance] getUserID];
    NSString *string_token = [[Model_user sharedInstance] getToken];
    
    NSMutableArray *array_sort = [NSMutableArray arrayWithObjects:@"getanswinfolawy",string_uid,myList_startNum,myList_displaynum,string_token,Model,Version,nil];
    NSString *string_sign = [self getSignWithArray:array_sort];
    
    NSString *string_url = [KWNBaseURL stringByAppendingString:[NSString stringWithFormat:@"lawyer/getanswinfolawy?method=getanswinfolawy&id=%@&start=%@&displaynum=%@&token=%@&src=%@&edition=%@&sig=%@",string_uid,myList_startNum,myList_displaynum,string_token,Model,Version,string_sign]];
    
    MKNetworkOperation* op = [self.kflEngine operationWithURLString:string_url];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSError *error;
            
            NSDictionary *kindDic = [NSJSONSerialization JSONObjectWithData:completedOperation.responseData options:NSJSONReadingMutableLeaves error:&error];
            
            NSNumber *number_code = [kindDic objectForKey:@"code"];
            NSString *string_message = [kindDic objectForKey:@"msg"];
            NSMutableArray *array_result = [NSMutableArray array];
            
            if(!number_code || !string_message){
                [array_result addObject:@"系统发生错误"];
            }
            else{
                if (number_code.integerValue == 10000) {
                    NSArray *array_data_tmp = [kindDic objectForKey:@"data"];
                    array_result = [self returnMyListDataArr:array_data_tmp];
//                    array_result = [KWNDealData deal_questListDataWithDicArray:array_data_tmp];
                }
                else{
                    [array_result addObject:string_message];
                }
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                resultBlock(number_code.integerValue,array_result);
            });
        });
    }errorHandler:^(MKNetworkOperation *completedOperation,NSError *error) {
        NSMutableArray *array_data = [NSMutableArray arrayWithObject:@"获取失败"];
        resultBlock(10001,array_data);
    }];
    [self.kflEngine enqueueOperation:op];
}

- (NSMutableArray *)returnMyListDataArr:(NSArray *)arr
{
    NSMutableArray *array = [[NSMutableArray alloc] init];

    for (int i = 0; i< arr.count; i++) {
        Model_myList *mylist = [[Model_myList alloc] init];
        NSDictionary *dic = [arr objectAtIndex:i];

        [mylist getSelfWithDic:dic];
        [array addObject:mylist];
    }
    
    
    return array;
}



@end
